package com.wwwarehouse.common.custom_widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.wwwarehouse.common.R;

public class CircleProgressView extends View {

	Context mContext = null;
	Paint mPaintJinji = null; // 紧急笔头
	Paint mPaintNormal; // 普通笔头
	Paint mPaintTimeout = null; // 超时笔头
	int dp4 = 0;
	Handler handler = null;
	float drawDegree = 0;
	public CircleProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public CircleProgressView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context c) {
		mContext = c;
		
		//紧急笔头设置
		mPaintJinji = new Paint();
		mPaintJinji.setStyle(Paint.Style.STROKE);
		mPaintJinji.setColor(mContext.getResources().getColor(R.color.task_red));
		mPaintJinji.setStrokeWidth(dip2px(mContext, 8));
		mPaintJinji.setAntiAlias(true);
		
		// 普通笔头设置
		mPaintNormal = new Paint();
		mPaintNormal.setStyle(Paint.Style.STROKE);
		mPaintNormal.setColor(mContext.getResources().getColor(R.color.task_blue));
		mPaintNormal.setStrokeWidth(dip2px(mContext, 8));
		mPaintNormal.setAntiAlias(true);
		
		// 超时笔头设置
		mPaintTimeout = new Paint();
		mPaintTimeout.setStyle(Paint.Style.STROKE);
		mPaintTimeout.setColor(mContext.getResources().getColor(R.color.task_yellow));
		mPaintTimeout.setStrokeWidth(dip2px(mContext, 8));
		mPaintTimeout.setAntiAlias(true);
		handler = new Handler();
		dp4 = dip2px(mContext, 4);
	}
	
	int radius; // 半径
	int countNormal = 0; // normal 数据个数
	int countJinji = 0; // 紧急数据个数
	int countTimeOut = 0; // 超时数据个数
	int countTotal = 0; // 总个数
	int startAngle = -90; // 画圆的开始角度

	@Override
	protected void onDraw(final Canvas canvas) {
		super.onDraw(canvas);
		int x = getWidth() / 2;
		int y = getHeight() / 2;
		radius = getWidth() / 2 - dp4;

		// 画正常的圆圈
		canvas.drawCircle(x, y, radius, mPaintNormal);

		if (countTotal != 0) { // 避免除0错
			// 画紧急的任务圆弧
			RectF rect = new RectF(getWidth()/2 - radius, dp4, getWidth()/2 + radius, getHeight() - dp4);
			if (drawDegree <= 360 * countJinji / countTotal) {
				canvas.drawArc(rect, startAngle, drawDegree, false, mPaintJinji);
			}

			// 画超时任务圆弧
			startAngle += 360 * countJinji / countTotal;
			rect = new RectF(getWidth()/2 - radius, dp4, getWidth()/2 + radius, getHeight() - dp4);
			if (drawDegree <= 360 * countTimeOut / countTotal) {
				canvas.drawArc(rect, startAngle, drawDegree, false, mPaintTimeout);
			}
		}
	}
	
	public int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }

	// 设置数据
	public void setData(int total, int normal, int jinji, int timeout) {
		try {
			countTotal = total;
			countNormal = normal;
			countJinji = jinji;
			countTimeOut = timeout;

			drawDegree = 360 * countJinji / countTotal > 360 * countTimeOut / countTotal ? 360 * countJinji / countTotal : 360 * countTimeOut / countTotal;
			ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, drawDegree);
			valueAnimator.setDuration(1000);
			// 匀速线性插值器
			valueAnimator.setInterpolator(new LinearInterpolator());
			valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				@Override
				public void onAnimationUpdate(ValueAnimator animation) {
					// 当插值计算进行时，获取中间的每个值，
					// 这里这个值是中间过程中的曲线长度（下面根据这个值来得出中间点的坐标值）
					drawDegree = (Float) animation.getAnimatedValue();
					invalidate();
				}
			});
			// 开始动画
			valueAnimator.start();

			ValueAnimator totalAnimator = ValueAnimator.ofFloat(0, total);
			totalAnimator.setDuration(1000);
			// 匀速线性插值器
			totalAnimator.setInterpolator(new LinearInterpolator());
			totalAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				@Override
				public void onAnimationUpdate(ValueAnimator animation) {
					// 当插值计算进行时，获取中间的每个值，
					// 这里这个值是中间过程中的曲线长度（下面根据这个值来得出中间点的坐标值）
					float num = (Float) animation.getAnimatedValue();
					if (onTotalNumberAnim != null) {
						onTotalNumberAnim.onAnim(num);
					}
				}
			});
			// 开始动画
			totalAnimator.start();
		}
		catch (Exception e) {

		}
	}

	public interface OnTotalNumberAnim {
		void onAnim(float num);
	}

	OnTotalNumberAnim onTotalNumberAnim = null;

	public void setOnTotalNumberAnim(OnTotalNumberAnim onAnim) {
		onTotalNumberAnim = onAnim;
	}
}
